﻿using System;
using System.IO;
using System.Web;
using Rabbit.Logic;
using RabbitManage.Code;
using Raven.Imports.Newtonsoft.Json;

namespace RabbitManage.Action
{
    public partial class OrderSent : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string id = Request["id"];
            if (string.IsNullOrEmpty(id)) return;

            HttpCookie cookie = Request.Cookies[Constant.CookieName];
            string shopId = cookie.Values[Constant.CookieUid];

            Stream postData = Request.InputStream;
            StreamReader sRead = new StreamReader(postData);
            string postContent = sRead.ReadToEnd();
            sRead.Close();

            if (!string.IsNullOrEmpty(postContent))
            {
                try
                {
                    Shipment shipment = JsonConvert.DeserializeObject<Shipment>(postContent);
                    if (shipment == null)
                    {
                        Response.Write("shipment null");
                        return;
                    }

                    if (shipment.CompanyName == null)
                    {
                        Response.Write("CompanyName null");
                        return;
                    }

                    string error = CriticalLogic.OrderSetShipment(Db.Store, id, shopId, shipment);
                    if (!string.IsNullOrEmpty(error))
                    {
                        Response.Write(error);
                        return;
                    }

                    Response.Write("true");
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }
            }
            else
            {
                Response.Write("false");
            }
        }
    }
}